package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCarModelMapper;
import org.linlinjava.litemall.db.dao.LitemallCarModelMapper;
import org.linlinjava.litemall.db.dao.LitemallCarModelMapper1;
import org.linlinjava.litemall.db.domain.LitemallCarModel;
import org.linlinjava.litemall.db.domain.LitemallCarModelExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallCarModelService {
    @Resource
    private LitemallCarModelMapper modelMapper;
    @Resource
    private LitemallCarModelMapper1 modelMapper1;
    private LitemallCarModel.Column[] columns = new LitemallCarModel.Column[]{LitemallCarModel.Column.id, LitemallCarModel.Column.name, LitemallCarModel.Column.group, LitemallCarModel.Column.image};

    public List<LitemallCarModel> query(Integer page, Integer limit, String sort, String order) {
        LitemallCarModelExample example = new LitemallCarModelExample();
        example.or().andDeletedEqualTo(String.valueOf(false));
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return modelMapper.selectByExampleSelective(example, columns);
    }

    public List<LitemallCarModel> selectModelList(String seriesId) {
        LitemallCarModelExample example = new LitemallCarModelExample();
        example.or().andSeriesIdEqualTo(Integer.valueOf(seriesId));
        example.or().andDeletedEqualTo(String.valueOf(false));
        return modelMapper.selectByExampleSelective(example, columns);
    }



    public List<LitemallCarModel> query(Integer page, Integer limit) {
        return query(page, limit, null, null);
    }

    public LitemallCarModel findById(Integer id) {
        return modelMapper.selectByPrimaryKey(id);
    }

    public List<LitemallCarModel> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
        LitemallCarModelExample example = new LitemallCarModelExample();
        LitemallCarModelExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(String.valueOf(false));

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return modelMapper.selectByExample(example);
    }

    public int updateById(LitemallCarModel brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return modelMapper.updateByPrimaryKeySelective(brand);
    }

    public void deleteById(Integer id) {
        modelMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallCarModel brand) {
        brand.setAddTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        modelMapper1.insert(brand);
    }

    public List<LitemallCarModel> all() {
        LitemallCarModelExample example = new LitemallCarModelExample();
        example.or().andDeletedEqualTo(String.valueOf(false));
        return modelMapper.selectByExample(example);
    }
}
